﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ScheduleMaker
{
    public partial class smAdmHourEdit : Form
    {
        private DB.Domain.Core.ClassHour hourToEdit = null;
        private DB.Domain.Core.ClassHour previousHour = null;
        private DB.Domain.Core.ClassHour nextHour = null;
        public smAdmHourEdit(DB.Domain.Core.ClassHour classHour_p, DB.Domain.Core.ClassHour previous_p, DB.Domain.Core.ClassHour next_p)
        {
            InitializeComponent();
            this.hourToEdit = classHour_p;
            this.previousHour = previous_p;
            this.nextHour = next_p;
            this.AddNewHourStart.Value = this.hourToEdit.Begin.Hours;
            this.AddNewMinuteStart.Value = this.hourToEdit.Begin.Minutes;
            this.AddNewHourEnd.Value = this.hourToEdit.End.Hours;
            this.AddNewMinuteEnd.Value = this.hourToEdit.End.Minutes;
            this.IDnumericUpDown1.Value = this.hourToEdit.Id;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            DB.CoreDAO.ClassHourDAO classHourDao = DB.DAOFactory.getClassHourDAO(ConfigManager.ConfigManager.getConfiguration().Type);

            //DB.Domain.ClassHourSchool cHour = new DB.Domain.ClassHourSchool();
            TimeSpan new_begin = new TimeSpan(Convert.ToInt32(AddNewHourStart.Value), Convert.ToInt32(AddNewMinuteStart.Value), 0);
            TimeSpan new_end = new TimeSpan(Convert.ToInt32(AddNewHourEnd.Value), Convert.ToInt32(AddNewMinuteEnd.Value), 0);
            if (new_begin < new_end && (this.previousHour == null || new_begin >= this.previousHour.End)
                && (this.nextHour == null || new_end <= this.nextHour.Begin))
            {
                this.hourToEdit.Begin = new_begin;
                this.hourToEdit.End = new_end;
                classHourDao.update(this.hourToEdit);
                this.Dispose();
            }
            else
            {
                MessageBox.Show("Nie można dodać takiej godziny!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void smAdmHourEdit_Load(object sender, EventArgs e)
        {

        }
    }
}
